<?php
class ModelGalleryContent extends Model {
	public function addGallery($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "gallery SET date_available = '" . $this->db->escape($data['date_available']) . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");
		
		$gallery_id = $this->db->getLastId();
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "gallery SET image = '" . $this->db->escape($data['image']) . "' WHERE gallery_id = '" . (int)$gallery_id . "'");
		}
		
		foreach ($data['gallery_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "gallery_description SET gallery_id = '" . (int)$gallery_id . "', language_id = '" . (int)$language_id . "', web_link = '" . $this->db->escape($value['web_link']) . "', name = '" . $this->db->escape($value['name']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		/*
		if (isset($data['gallery_image'])) {
			foreach ($data['gallery_image'] as $image) {
        		$this->db->query("INSERT INTO " . DB_PREFIX . "gallery_image SET gallery_id = '" . (int)$gallery_id . "', image = '" . $this->db->escape($image) . "'");
			}
		}
		*/
		if (isset($data['gallery_category'])) {
			foreach ($data['gallery_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "gallery_to_category SET gallery_id = '" . (int)$gallery_id . "', category_id = '" . (int)$category_id . "'");
			}
		}
      
		$this->cache->delete('gallery');
	}
	
	public function editGallery($gallery_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "gallery SET date_available = '" . $this->db->escape($data['date_available']) . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE gallery_id = '" . (int)$gallery_id . "'");

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "gallery SET image = '" . $this->db->escape($data['image']) . "' WHERE gallery_id = '" . (int)$gallery_id . "'");
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "gallery_description WHERE gallery_id = '" . (int)$gallery_id . "'");
		
		foreach ($data['gallery_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "gallery_description SET gallery_id = '" . (int)$gallery_id . "', language_id = '" . (int)$language_id . "', web_link = '" . $this->db->escape($value['web_link']) . "', name = '" . $this->db->escape($value['name']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
        /*
		$this->db->query("DELETE FROM " . DB_PREFIX . "gallery_image WHERE gallery_id = '" . (int)$gallery_id . "'");
		
		if (isset($data['gallery_image'])) {
			foreach ($data['gallery_image'] as $image) {
        		$this->db->query("INSERT INTO " . DB_PREFIX . "gallery_image SET gallery_id = '" . (int)$gallery_id . "', image = '" . $this->db->escape($image) . "'");
			}
		}
		*/
		$this->db->query("DELETE FROM " . DB_PREFIX . "gallery_to_category WHERE gallery_id = '" . (int)$gallery_id . "'");
		
		if (isset($data['gallery_category'])) {
			foreach ($data['gallery_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "gallery_to_category SET gallery_id = '" . (int)$gallery_id . "', category_id = '" . (int)$category_id . "'");
			}		
		}

		$this->cache->delete('gallery');
	}
	
	public function deleteGallery($gallery_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "gallery WHERE gallery_id = '" . (int)$gallery_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "gallery_description WHERE gallery_id = '" . (int)$gallery_id . "'");
		
        //$this->db->query("DELETE FROM " . DB_PREFIX . "gallery_image WHERE gallery_id = '" . (int)$gallery_id . "'");
		
        $this->db->query("DELETE FROM " . DB_PREFIX . "gallery_to_category WHERE gallery_id = '" . (int)$gallery_id . "'");	
		$this->cache->delete('gallery');
	}
	
	public function getGallery($gallery_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'gallery_id=" . (int)$gallery_id . "') AS keyword FROM " . DB_PREFIX . "gallery p LEFT JOIN " . DB_PREFIX . "gallery_description pd ON (p.gallery_id = pd.gallery_id) WHERE p.gallery_id = '" . (int)$gallery_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
				
		return $query->row;
	}
	
	public function getGallerys($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "gallery p LEFT JOIN " . DB_PREFIX . "gallery_description pd ON (p.gallery_id = pd.gallery_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'"; 
		
			if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
				$sql .= " AND LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($data['filter_name'])) . "%'";
			}
           
			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
			}

			$sort_data = array(
				'pd.name',
				'p.status',
				'p.sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY pd.name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$product_data = $this->cache->get('gallery.' . $this->config->get('config_language_id'));
		
			if (!$product_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery p LEFT JOIN " . DB_PREFIX . "gallery_description pd ON (p.gallery_id = pd.gallery_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY pd.name ASC");
	
				$product_data = $query->rows;
			
				$this->cache->set('gallery.' . $this->config->get('config_language_id'), $product_data);
			}	
	
			return $product_data;
		}
	}
    
	public function getGallerysByKeyword($keyword) {
		if ($keyword) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery p LEFT JOIN " . DB_PREFIX . "gallery_description pd ON (p.gallery_id = pd.gallery_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND (LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($keyword)) . "%')");
									  
			return $query->rows;
		} else {
			return array();	
		}		
	} 
	
	public function getGallerysByCategoryId($category_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery p LEFT JOIN " . DB_PREFIX . "gallery_description pd ON (p.gallery_id = pd.gallery_id) LEFT JOIN " . DB_PREFIX . "gallery_to_category p2c ON (p.gallery_id = p2c.gallery_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2c.category_id = '" . (int)$category_id . "' ORDER BY pd.name ASC");
								  
		return $query->rows;
	} 
	
	public function getGalleryDescriptions($gallery_id) {
		$gallery_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery_description WHERE gallery_id = '" . (int)$gallery_id . "'");
		
		foreach ($query->rows as $result) {
			$gallery_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_description' => $result['meta_description'],
                'web_link'         => $result['web_link'],
				'description'      => $result['description']
			);
		}
		
		return $gallery_description_data;
	}
   /*
	public function getGalleryImages($gallery_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery_image WHERE gallery_id = '" . (int)$gallery_id . "'");
		
		return $query->rows;
	}
    */
	public function getGalleryCategories($gallery_id) {
		$gallery_category_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery_to_category WHERE gallery_id = '" . (int)$gallery_id . "'");
		
		foreach ($query->rows as $result) {
			$gallery_category_data[] = $result['category_id'];
		}

		return $gallery_category_data;
	}
    
	public function getTotalGallery($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "gallery p LEFT JOIN " . DB_PREFIX . "gallery_description pd ON (p.gallery_id = pd.gallery_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($data['filter_name'])) . "%'";
		}
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
		}

		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}	

	public function getTotalGallerysByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "gallery WHERE image_id = '" . (int)$image_id . "'");

		return $query->row['total'];
	}
}
?>